// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,

    properties: {
        info : {
            default: null,
            type: cc.Prefab
        },
        player: {
            default: null,
            type: cc.Prefab
        },
        playerAi: {
            default: null,
            type: cc.Prefab
        },
        actor1: {
            default: null,
            type: cc.Prefab
        },
        actor2: {
            default: null,
            type: cc.Prefab
        },
        actor3: {
            default: null,
            type: cc.Prefab
        },
        actor4: {
            default: null,
            type: cc.Prefab
        },
        bgs1: {
            default: null,
            type: cc.Prefab
        },
        bgs2: {
            default: null,
            type: cc.Prefab
        },
        bgs3: {
            default: null,
            type: cc.Prefab
        },
        bgsend: {
            default: null,
            type: cc.Prefab
        },
        killItem : {
            default: null,
            type: cc.Prefab
        }
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {
        this._init();
    },

    onLoad () {
        app.bg = this;
        //镜头最开始的位置保存 每次游戏开始的时候使用
        this.camerainitP = {x: app.camera.x, y: app.camera.y};
        let bgss = this.bgss = [];
        bgss.push(this.bgs1);
        bgss.push(this.bgs2);
        bgss.push(this.bgs3);
        // bgss.push(this.bgs4);
    },
    _init: function () {

        app.camera.x = this.camerainitP.x;
        app.camera.y = this.camerainitP.y;

        for (var i = 0; i < this.node.children.length; i++) {
            this.node.children[i].destroy();
        }

        let actorps =this.actorps= UtilsNode.startActor(function (bgArr,postions) {
            console.log("背景数组", bgArr);
            this.bgArr = bgArr;

            this.initBg(postions);
        }.bind(this));


        this.actors = [];
        this.actors.push(this.actor1);
        this.actors.push(this.actor2);
        this.actors.push(this.actor3);
        this.actorsNode = UtilsNode.actorBirth(this.node, actorps, this.actors, this.actors[0], this
            .actor4);
        console.log("要创建东西的位置", actorps);
        this.addInfo(actorps);

        let node = this.playerNode = cc.instantiate(this.player);
        this.node.addChild(node);
        node.zIndex = 9999999;


        this.playerNode.getComponent("player").setMap(this.actorsNode,0);




        this._initAi();

        this.callback();


        this.playerNode.getComponent("player").startGame(false);
        this.playerNodeAi.getComponent("player").startGame(false);
        if (app.readGo != null) {
            app.readGo.readgo(function () {
                this.playerNode.getComponent("player").startGame(true);
                this.playerNodeAi.getComponent("player").startGame(true);
            }.bind(this));
        }


    },
    //    初始化ai
    _initAi : function(){
        let nodeAi = this.playerNodeAi = cc.instantiate(this.playerAi);
        this.node.addChild(nodeAi);
        this.playerNodeAi.getComponent("player").setMap(this.actorsNode,1);

        this.unschedule(this.callbacks);
        this.callbacks = function () {
            // if (this.car.x<-165) {
            //     // 在第六次执行回调时取消这个计时器
            //     this.unschedule(this.callback);
            // }
            // let x = UtilsNode.getRandom(2,0,1);
            //
            // if (x == 0) {
            //     this.playerNodeAi.getComponent("player").addOne();
            // }else if (x == 1) {
            //     this.playerNodeAi.getComponent("player").addTwo();
            // }
            this.playerNodeAi.getComponent("player").addAuto();
        }
        this.schedule(this.callbacks, 0.8);






    },


    //两步提示
    addInfo: function (actorps) {
        this.info = cc.instantiate(this.info);
        this.info.active = true;
        this.node.addChild(this.info);
        this.info.zIndex = 999999;
        // console.log("提示在node 里面有没有",this.node.children);
        this.info.setPosition(actorps[7].x, actorps[7].y+100);
        let  sequence = cc.sequence(cc.moveTo(0.2, cc.v2(actorps[7].x-20,actorps[7].y+100)),cc.moveTo(0.2, cc.v2(actorps[7].x+20,actorps[7].y+100)));
        // let  sequence = cc.moveTo(1, cc.v2(actorps[6].x-10,actorps[6].y));
        var myAction = cc.repeatForever(sequence);
        this.info.runAction(myAction)
    },

    initBg: function (postions) {
        //创建 荷花啥的
        for (var i = 0; i < this.bgArr.length; i++) {

            if (i % 2 == 0) {

                //偶数

               let node = this.addRandomBG()

                node.setPosition(this.bgArr[i].x - 350, this.bgArr[i].y);
                UtilsNode.setMyRotation(1,node);
                // this.addRandomBG().setPosition(this.bgArr[i].x - 350, this.bgArr[i].y);
                // this.addRandomBG().setPosition(this.bgArr[i].x - 500, this.bgArr[i].y);

            } else {
                //奇数
                    let node = this.addRandomBG()
                node.setPosition(this.bgArr[i].x + 350, this.bgArr[i].y);
                UtilsNode.setMyRotation(0,node);
                // this.addRandomBG().setPosition(this.bgArr[i].x + 350, this.bgArr[i].y);
                // this.addRandomBG().setPosition(this.bgArr[i].x + 500, this.bgArr[i].y);
            }
        }
        // this.addBG(2).setPosition(postions[postions.length-1].x - 200, postions[postions.length-1].y+200);
        // this.addBG(2).setPosition(postions[postions.length-1].x + 200, postions[postions.length-1].y+250);
        //
        // this.addBG(2).setPosition(postions[postions.length-1].x - 220, postions[postions.length-1].y+400);
        // this.addBG(2).setPosition(postions[postions.length-1].x + 180, postions[postions.length-1].y+450);
        //
        // this.addBG(2).setPosition(postions[postions.length-1].x - 300, postions[postions.length-1].y+500);
        // this.addBG(2).setPosition(postions[postions.length-1].x - 150, postions[postions.length-1].y+600);
        // // this.addBG(2).setPosition(postions[postions.length-1].x + 200, postions[postions.length-1].y+600);
        //
        // this.add1BG(6).setPosition(postions[postions.length-1].x+200, postions[postions.length-1].y+600);
        let bgsend = cc.instantiate(this.bgsend);
        bgsend.setPosition(postions[postions.length-1].x+200, postions[postions.length-1].y+500);
        this.node.addChild(bgsend);

    },

    addRandomBG : function(Scale){
        let index = UtilsNode.getRandom(0, 0, this.bgss.length);
        let node = this.playerNode = cc.instantiate(this.bgss[index]);
        if (Scale!=null) {
            node.setScale(Scale);
        }else{
            node.setScale(1);
        }

        this.node.addChild(node);
        return node;
    },
    add1BG : function(Scale){
        let node = this.playerNode = cc.instantiate(this.bgss[1]);
        node.setScale(6);
        this.node.addChild(node);
        return node;
    },
    addBG : function(Scale){
        let index = UtilsNode.getRandom(2, 1, 2);
        let node = this.playerNode = cc.instantiate(this.bgss[index]);
        if (Scale!=null) {
            node.setScale(Scale);
        }else{
            node.setScale(1);
        }
        this.node.addChild(node);
        return node;
    },


    callback: function () {
        this.playerNode.getComponent("player").setCallBackGameOver(function () {
            console.log("游戏结束了");

            app.titlelayout.hideButton(true);
            this.playerNodeAi.getComponent("player").winOrLose(false);
            app.bgm.playimwin();
            // app.bgm.stop();
        }.bind(this));
        this.playerNode.getComponent("player").setCallBackTrap(function () {
            // console.log("掉陷阱");
            app.bgm.playluoshui();
            app.bgm.playa();
        }.bind(this));
        this.playerNode.getComponent("player").setCallBackMove(function (index) {

            this.playerIndex = index;
            this.isDisparity();
            console.log("移动了了吗",index);
            app.titlelayout.setProgress(0,index,this.actorps.length-1);
        }.bind(this));
        this.playerNode.getComponent("player").setCallBackInfo(function () {
            console.log("info消失");
            this.info.active = false;
        }.bind(this));
        this.playerNode.getComponent("player").setCallBackJump(function (p) {
            // let node  = cc.instantiate(this.killItem);
            // let node = UtilsNode.getNode("qi",this.playerNode);
            // node.getComponent("kill").setActive(true);
            // this.playerNode.getComponent("player").showsSmoke(true);
            // node.setPosition(p.x, p.y);
        }.bind(this));
        this.playerNode.getComponent("player").setCallBackCameraMove(function (node) {
            //镜头移动
            if (node.y>500) {
                if (node.y-500 > app.camera.y) {
                    var myAction =cc.moveTo(0.2, cc.v2(app.camera.x,node.y-500))
                    app.camera.runAction(myAction);
                }

            }
        }.bind(this));


        this.playerNodeAi.getComponent("player").setCallBackGameOver(function () {
            console.log("游戏结束了");
            app.titlelayout.hideButton(true);
            this.playerNode.getComponent("player").winOrLose(false);
            // app.bgm.playkuaidaozhongdian();
            app.bgm.playzhecibusuan();
        }.bind(this));
        this.playerNodeAi.getComponent("player").setCallBackTrap(function () {
            // console.log("掉陷阱");
            app.bgm.playluoshui();
            app.bgm.playa();
        }.bind(this));
        this.playerNodeAi.getComponent("player").setCallBackMove(function (index) {

            this.playerNodeAiIndex = index;
            app.titlelayout.setProgress(1,index,this.actorps.length-1);
            // console.log("移动了",index);
            this.isDisparity();
        }.bind(this));
        this.playerNodeAi.getComponent("player").setCallBackInfo(function () {
            // console.log("info消失");
            // this.info.active = false;
        }.bind(this));
        this.playerNodeAi.getComponent("player").setCallBackJump(function (p) {
            // let node  = cc.instantiate(this.killItem);
            // let node = UtilsNode.getNode("qi",this.playerNodeAi);
            // //落地灰尘显示
            // node.getComponent("kill").setActive(true);
            // this.playerNodeAi.getComponent("player").showsSmoke(true);
            // node.setPosition(p.x, p.y);
        }.bind(this));



        //初始化相差步数
        this.playerNodeAiIndex = 0;
        this.playerIndex = 0;
        //如有对象 就默认隐藏
        if (app.titlelayout != null) {
            app.titlelayout._init();
        }

    },

    isDisparity : function(){
        // console.log("info消失");
        let index = 0;

        if (this.playerNodeAiIndex-this.playerIndex>0) {
            index =this.playerNodeAiIndex-this.playerIndex;
            this.showDisparity(1,index);
        }else if (this.playerIndex-this.playerNodeAiIndex>0) {

            index =this.playerIndex-this.playerNodeAiIndex;
            this.showDisparity(0,index);
        }
    },

    showDisparity : function(type , index){
        if (index>10) {
            app.titlelayout.showinfo(true,type,index);

        }else{
            app.titlelayout.showinfo(false,type,index);
        }

        // switch (type) {
        //     case 0:
        //         console.log("你领先",index);
        //
        //         break;
        //     case 1:
        //         console.log("你落后",index);
        //         break;
        // }
    },


    one: function () {
        this.playerNode.getComponent("player").addOne();


    },
    two: function () {
        this.playerNode.getComponent("player").addTwo();

    },
    reset: function () {
        this._init();
    }
    // update (dt) {},
});
